package com.whnk.attendance.controller;

import com.whnk.attendance.service.IProjectEmpInfoService;
import com.whnk.attendance.vo.ProjectEmpInfoListVo;
import com.whnk.attendance.vo.ProjectEmpInfoQueryVo;
import com.whnk.attendance.vo.ProjectEmpInfoUpdateVo;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import com.whnk.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/projectEmpInfo")
@Api(tags="项目人员列列表")
public class ProjectEmpInfoController extends BaseController {

    @Autowired
    private IProjectEmpInfoService projectEmpInfoService;

    /**
     * 分页查询项目人员列表
     */
    @PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询项目人员列表" , notes = "分页查询项目人员列表")
    public TableDataInfo list(@ApiParam(value = "项目人员信息", required = false) ProjectEmpInfoQueryVo projectEmpInfoQueryVo)
    {
        startPage();
        List<ProjectEmpInfoListVo> list = projectEmpInfoService.selectProjectEmpInfoListVoList(projectEmpInfoQueryVo);
        return getDataTable(list);
    }

    /**
     * 根据逻辑主键查询项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping(value = "/query/{proEmpId}")
    @ApiOperation(value = "根据逻辑主键查询项目人员信息", notes = "根据逻辑主键查询项目人员信息")
    public AjaxResult getInfo(@ApiParam(value = "逻辑主键", required = false) @PathVariable("proEmpId") Long proEmpId)
    {
        return AjaxResult.success(projectEmpInfoService.selectProjectEmpInfoByProEmpId(proEmpId));
    }

    /**
     * 新增项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "项目人员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增项目人员信息", notes = "新增项目人员信息")
    public AjaxResult add(@ApiParam(value = "项目人员信息", required = false) @Validated @RequestBody ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo)
    {
        if(!projectEmpInfoService.checkPER(projectEmpInfoUpdateVo)){
            return AjaxResult.error("新增项目人员信息失败，该项目人员信息已存在");
        }
        return toAjax(projectEmpInfoService.insertProjectEmpInfo(projectEmpInfoUpdateVo));
    }

    /**
     * 修改项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "项目人员信息", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation(value = "修改项目人员信息", notes = "修改项目人员信息")
    public AjaxResult edit(@ApiParam(value = "项目人员信息", required = false) @Validated @RequestBody ProjectEmpInfoUpdateVo projectEmpInfoUpdateVo)
    {
        if(!projectEmpInfoService.checkPER(projectEmpInfoUpdateVo)){
            return AjaxResult.error("修改项目人员信息失败，该项目人员信息已存在");
        }
        return toAjax(projectEmpInfoService.updateProjectEmpInfo(projectEmpInfoUpdateVo));
    }

    /**
     * 批量删除项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "项目人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{proEmpIds}")
    @ApiOperation(value = "批量删除项目人员信息", notes = "批量删除项目人员信息")
    public AjaxResult remove(@ApiParam(value = "逻辑主键", required = false) @PathVariable Long[] proEmpIds)
    {
        return toAjax(projectEmpInfoService.deleteProjectEmpInfoByProEmpIds(proEmpIds));
    }

    /**
     * 导出项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "项目人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出项目人员信息", notes = "导出项目人员信息")
    public void export(HttpServletResponse response, @ApiParam(value = "项目人员信息", required = false) ProjectEmpInfoQueryVo projectEmpInfoQueryVo) throws IOException
    {
        List<ProjectEmpInfoListVo> list = projectEmpInfoService.selectProjectEmpInfoListVoList(projectEmpInfoQueryVo);
        ExcelUtil<ProjectEmpInfoListVo> util = new ExcelUtil<ProjectEmpInfoListVo>(ProjectEmpInfoListVo.class);
        util.exportExcel(response, list, "项目人员信息数据");
    }

    /**
     * 导入项目人员信息
     */
    @PreAuthorize(hasPermi = "attendance:info:import")
    @Log(title = "项目人员信息", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入项目人员信息", notes = "导入项目人员信息")
    public AjaxResult importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ProjectEmpInfoListVo> util = new ExcelUtil<>(ProjectEmpInfoListVo.class);
        List<ProjectEmpInfoListVo> projectEmpInfoList = util.importExcel(file.getInputStream());
        String message = projectEmpInfoService.importData(projectEmpInfoList, updateSupport);
        return AjaxResult.success(message);
    }

}
